#include "editmapdialog.h"
#include "ui_editmapdialog.h"

EditMapDialog::EditMapDialog(Map *aMap, const bool &aCurrent, QWidget *parent) :
    QDialog(parent), ui(new Ui::EditMapDialog), myMap(aMap)
{
    if(aMap)
    {
        QString tmpString = aMap->GetName();
        GeoCoordinates topLeft, topRight, bottomRight, bottomLeft;
        myMap->GetCalibration(topLeft, topRight, bottomRight, bottomLeft);
        ui->setupUi(this);
        // вывод на экран информации о редактируемой карте
        ui->mapNameLineEdit->setText(tmpString);
        tmpString = myMap->GetFilePath();
        ui->pathLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", topLeft.GetLatitude());
        ui->topLeftLatLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", topLeft.GetLongitude());
        ui->topLeftLongLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", topRight.GetLatitude());
        ui->topRightLatLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", topRight.GetLongitude());
        ui->topRightLongLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", bottomRight.GetLatitude());
        ui->bottomRightLatLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", bottomRight.GetLongitude());
        ui->bottomRightLongLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", bottomLeft.GetLatitude());
        ui->bottomLeftLatLineEdit->setText(tmpString);
        tmpString.sprintf("%lf", bottomLeft.GetLongitude());
        ui->bottomLeftLongLineEdit->setText(tmpString);
        // установка проверки диапазона вводимых координат
        QRegExp regExpLatitude("-?((90)|(([1-8]?[0-9]?)(\\056)[0-9]{0,10}))");                      // -90 ... 90
        QRegExp regExpLongitude("-?((180)|(((1[0-7][0-9])|([1-9]?[0-9]?))(\\056)[0-9]{0,10}))");    // -180 ... 180
        ui->topLeftLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
        ui->topLeftLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
        ui->topRightLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
        ui->topRightLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
        ui->bottomRightLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
        ui->bottomRightLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
        ui->bottomLeftLatLineEdit->setValidator(new QRegExpValidator(regExpLatitude, this));
        ui->bottomLeftLongLineEdit->setValidator(new QRegExpValidator(regExpLongitude, this));
        ui->setCurrentCheckBox->setChecked(aCurrent);
        ui->setCurrentCheckBox->setVisible(!aCurrent);
        setFixedSize(size());
    }
    else
        close();
}

EditMapDialog::~EditMapDialog()
{
    delete ui;
}

bool EditMapDialog::Result(bool &aChangeCurrentMap)
{
    if(IsEnabledToSave() && myMap)
    {
        myMap->ClearMap();
        myMap->SetName(ui->mapNameLineEdit->text());
        myMap->SetFilePath(ui->pathLineEdit->text());
        myMap->SetCalibration(GeoCoordinates(ui->topLeftLatLineEdit->text().toDouble(),       ui->topLeftLongLineEdit->text().toDouble()),
                             GeoCoordinates(ui->topRightLatLineEdit->text().toDouble(),       ui->topRightLongLineEdit->text().toDouble()),
                             GeoCoordinates(ui->bottomRightLatLineEdit->text().toDouble(),    ui->bottomRightLongLineEdit->text().toDouble()),
                             GeoCoordinates(ui->bottomLeftLatLineEdit->text().toDouble(),     ui->bottomLeftLongLineEdit->text().toDouble()));
        aChangeCurrentMap = ui->setCurrentCheckBox->isChecked();
        return true;
    }
    return false;
}

void EditMapDialog::on_openFileButton_clicked(void)
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Выбор изображения для создания карты"), "", tr("Изображения (*.png)"));
    ui->pathLineEdit->setText(fileName);
}

void EditMapDialog::on_pathLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_topLeftLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_topLeftLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_topRightLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_topRightLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_bottomRightLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_bottomRightLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_bottomLeftLatLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_bottomLeftLongLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

void EditMapDialog::on_mapNameLineEdit_textChanged(const QString& /*arg1*/)
{
    ui->saveButton->setEnabled(IsEnabledToSave());
}

bool EditMapDialog::CheckCoordinates(void) const
{
    GeoCoordinates topLeft(ui->topLeftLatLineEdit->text().toDouble(), ui->topLeftLongLineEdit->text().toDouble()),
                   topRight(ui->topRightLatLineEdit->text().toDouble(),       ui->topRightLongLineEdit->text().toDouble()),
                   bottomRight(ui->bottomRightLatLineEdit->text().toDouble(), ui->bottomRightLongLineEdit->text().toDouble()),
                   bottomLeft(ui->bottomLeftLatLineEdit->text().toDouble(), ui->bottomLeftLongLineEdit->text().toDouble());
    return (topLeft.GetLatitude() > bottomLeft.GetLatitude()    && topLeft.GetLatitude() > bottomRight.GetLatitude() &&
            topRight.GetLatitude() > bottomLeft.GetLatitude()   && topRight.GetLatitude() > bottomRight.GetLatitude() &&
            bottomRight.GetLongitude() > topLeft.GetLongitude() && bottomRight.GetLongitude() > bottomLeft.GetLongitude() &&
            topRight.GetLongitude() > topLeft.GetLongitude()    && topRight.GetLongitude() > bottomLeft.GetLongitude());
}

bool EditMapDialog::IsEnabledToSave(void) const
{
    return   (!ui->mapNameLineEdit->text().isEmpty() && !ui->pathLineEdit->text().isEmpty() &&
             (!ui->topLeftLatLineEdit->text().isEmpty() && ui->topLeftLatLineEdit->text().compare(".") &&
             ui->topLeftLatLineEdit->text().compare("-") && ui->topLeftLatLineEdit->text().compare("-.")) &&
             (!ui->topLeftLongLineEdit->text().isEmpty() && ui->topLeftLongLineEdit->text().compare(".") &&
             ui->topLeftLongLineEdit->text().compare("-") && ui->topLeftLongLineEdit->text().compare("-.")) &&
             (!ui->topRightLatLineEdit->text().isEmpty() && ui->topRightLatLineEdit->text().compare(".") &&
             ui->topRightLatLineEdit->text().compare("-") && ui->topRightLatLineEdit->text().compare("-.")) &&
             (!ui->topRightLongLineEdit->text().isEmpty() && ui->topRightLongLineEdit->text().compare(".") &&
             ui->topRightLongLineEdit->text().compare("-") && ui->topRightLongLineEdit->text().compare("-.")) &&
             (!ui->bottomRightLatLineEdit->text().isEmpty() && ui->bottomRightLatLineEdit->text().compare(".") &&
             ui->bottomRightLatLineEdit->text().compare("-") && ui->bottomRightLatLineEdit->text().compare("-.")) &&
             (!ui->bottomRightLongLineEdit->text().isEmpty() && ui->bottomRightLongLineEdit->text().compare(".") &&
             ui->bottomRightLongLineEdit->text().compare("-") && ui->bottomRightLongLineEdit->text().compare("-.")) &&
             (!ui->bottomLeftLatLineEdit->text().isEmpty() && ui->bottomLeftLatLineEdit->text().compare(".") &&
             ui->bottomLeftLatLineEdit->text().compare("-") && ui->bottomLeftLatLineEdit->text().compare("-.")) &&
             (!ui->bottomLeftLongLineEdit->text().isEmpty() && ui->bottomLeftLongLineEdit->text().compare(".") &&
             ui->bottomLeftLongLineEdit->text().compare("-") && ui->bottomLeftLongLineEdit->text().compare("-.")) &&
             CheckCoordinates());
}
